package lightrain.code.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils;


public class WebContext {

	private HttpServletRequest request;
	
	private HttpServletResponse response;

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public WebContext(HttpServletRequest request, HttpServletResponse response) {
		super();
		this.request = request;
		this.response = response;
	}
	
	/**
	 * 获取ip地址
	 * @auther Y.hj
	 * @return
	 */
	public String getIpAddress() {
		String ip = request.getHeader("x-forwarded-for");
		if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
			ip = request.getHeader("Proxy-Client-IP");
		if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
			ip = request.getHeader("WL-Proxy-Client-IP");
		if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
			ip = request.getRemoteAddr();
		return ip;
	}

	/**
	 * 获取操作系统信息
	 * @auther Y.hj
	 * @return
	 */
	public String getRequestOS() {
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		String[] syss = {"iphone","android","ipad","wp"};
		if(StringUtils.isEmpty(userAgent))
			return "other";
		for (int i = 0; i < syss.length; i++)
			if(userAgent.indexOf(syss[i]) != -1)
				return syss[i];
		return "other";
	}

	/**
	 * path
	 * @auther Y.hj
	 * @return
	 */
	public String path() {
		return request.getServletPath();
	}


	/**
	 * 获取contextpath
	 * @auther Y.hj
	 * @return
	 */
	public String getContextPath() {
		return request.getContextPath();
	}


	/**
	 * 
	 * @auther Y.hj
	 * @param name
	 * @param value
	 * @return
	 */
	public WebContext sessionAttr(String name, Object value) {
		WebUtils.setSessionAttribute(request, name, value);
		return this;
	}


	/**
	 * @auther Y.hj
	 * @param name
	 * @return
	 */
	public Cookie getCookie(String name) {
		return WebUtils.getCookie(request, name);
	}

	public String getCookieValue(String name) {
		Cookie cookie = getCookie(name);
		if(cookie != null)
			return cookie.getValue();
		return null;
	}

	public String encodeUrl(String url) {
		return response.encodeURL(url);
	}

	public String decodeUrl(String url) {
		try {
			return URLDecoder.decode(url, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public WebContext removeSession(String name) {
		request.getSession().removeAttribute(name);
		return this;
	}

	/**
	 * 
	 * @auther Y.hj
	 * @return
	 */
	public String getHost() {
		return request.getScheme()+"://"+request.getServerName();
	}

	public String getHostAndPort() {
		return getHost()+request.getRemotePort();
	}

	public String getLocationHost() {
		return request.getScheme()+"://"+request.getServerName();
	}

	public void removeCookie(String name) {
		addCookie(name, "", 0);
	}
	
	public void addCookie(String name, String value) {
		addCookie(name, value, 1000*60*60*24*365*10);		
	}

	public void addCookie(String name, String value, int expiry) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(expiry);
		cookie.setPath("/");
		addCookie(cookie);		
	}

	public void addCookie(Cookie cookie) {
		response.addCookie(cookie);
	}
	
	
}
